package io;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

/**
 * Generic data class, loaded from a JSON stream.
 * @author G'lek
 * @version 0.0.1
 */
public class Data {
    
    private JSONObject data;
    
    /**
     * Blank Data
     */
    public Data(){
        data = new JSONObject();
    }
    
    /**
     * Data loaded from a file
     * @param input The InputStream to read from
     * @throws IOException If reading fails
     * @throws ParseException If not a valid JSON string
     */
    public Data(InputStream input) throws IOException, ParseException{
        JSONParser parser = new JSONParser();
        InputStreamReader reader = new InputStreamReader(input);
        data = (JSONObject)parser.parse(reader);
    }
    
    /**
     * Get a value
     * @param key The key of the value
     * @return The value
     */
    public Object getValue(String key){
        return data.get(key);
    }
    
    /**
     * Set the value for a key
     * @param key
     * @param value 
     */
    public void setValue(String key, Object value){
        data.put(key, value);
        return;
    }
    
    public String getType(){
        return (String)data.get("DATATYPE");
    }
    
    public String toJSONString(){
        return data.toJSONString();
    }
}
